package likou;

import org.omg.PortableServer.LIFESPAN_POLICY_ID;

import java.util.*;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/4/25  19:51
 */
public class _46 {
    public static void main(String[] args) {
        int[] nums = {1,2,3};
        permute(nums).forEach(System.out::println);
    }
    public static List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        int len = nums.length;
        if (len==0){
            return res;
        }
        Deque<Integer> path = new ArrayDeque<Integer>();
        boolean[] used = new boolean[len];
        dfs(nums,len,0,path,used,res);
        return res;
    }

    private static void dfs(int[] nums, int len, int depth, Deque<Integer> path, boolean[] used, List<List<Integer>> res) {
        if (depth==len){
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i=0;i<len;i++){
            if (used[i]){
                continue;
            }
            path.addLast(nums[i]);
            used[i] = true;
            dfs(nums,len,depth+1,path,used,res);
            path.removeLast();
            used[i] = false;
        }
    }
}
